package com.cbs.java.component.network.decoder;


import okhttp3.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by brochexu on 9/2/14.
 */
public class TextDecoder implements Decoder {
    private String encode = "UTF-8";

    public TextDecoder(String encode) {
        this.encode = encode;
    }

    public TextDecoder() {

    }

    public Object decode(ResponseBody body) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(body.byteStream(), encode));
        String line;
        StringBuilder sb = new StringBuilder();
        while (null != (line = br.readLine())) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        return sb.toString();
    }

    @Override
    public String desc(Object result) {
        return String.format("string(%d): %s", result.toString().length(), result.toString());
    }
}
